/**
 * <copyright>
 * </copyright>
 *
 * $Id$
 */
package Wires.impl;

import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

import org.eclipse.emf.ecore.EClass;

import Wires.ConnectableElement;
import Wires.IdentityTransformation;
import Wires.InputActualParameter;
import Wires.OutputActualParameter;
import Wires.WiresPackage;
import Wires.exep.IdOutParamNotFoundException;

/**
 * <!-- begin-user-doc -->
 * An implementation of the model object '<em><b>Identity Transformation</b></em>'.
 * <!-- end-user-doc -->
 * <p>
 * </p>
 *
 * @generated
 */
public class IdentityTransformationImpl extends AtomicModelTransformationImpl implements IdentityTransformation {
	/***************************************************/
	/***************** Modifications *******************/
	
	public void execute(ConnectableElement incoming) throws Exception{
		
		List<InputActualParameter> inparam = this.getInParams();
	
		boolean ready = true;
		for (InputActualParameter parameter : inparam){
			ready = ready && ((InputActualParameterImpl)parameter).getActive();
		}
		
		if (super.getActive() == true && ready){						
			for (InputActualParameter parameter : inparam){
				Iterator<OutputActualParameter> it = this.getOutParams().iterator();
				boolean found = false;
				OutputActualParameter outParam = null;
				while (it.hasNext() && !found){
					outParam = it.next();
					if (outParam.getName().equals(parameter.getName()))
						found = true;					
				}
				if (found){
					((OutputActualParameterImpl)outParam).setModelPath(((InputActualParameterImpl) parameter).getModelPath());
				}else{
					throw new IdOutParamNotFoundException(parameter);
				}
			}
			

			//Get the output parameters
			this.getOutParams();
			List<ConnectableElement> outParamlist = new LinkedList<ConnectableElement>();
			for (OutputActualParameter oap : this.getOutParams()){
				outParamlist.add(((ConnectableElement)oap));
			}
			
			super.setActive(false);
			//Call the main method from WiresSpecification
			WiresSpecificationImpl.execute(outParamlist, null);			
		}
	}
	
	/**************** Modifications End ****************/
	/***************************************************/
	
	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	protected IdentityTransformationImpl() {
		super();
	}

	/**
	 * <!-- begin-user-doc -->
	 * <!-- end-user-doc -->
	 * @generated
	 */
	@Override
	protected EClass eStaticClass() {
		return WiresPackage.Literals.IDENTITY_TRANSFORMATION;
	}

} //IdentityTransformationImpl
